
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import random
import re

import jwt
from django.contrib.auth.hashers import make_password
from django.http import HttpResponse
from rest_framework.response import Response
from rest_framework.views import APIView

from Django import settings
from users.ser import *
from utils.comm import send_message, get_redis
from users.models import *


# 测试
class Text(APIView):
    def get(self, request):
        conn = get_redis()
        conn.setex('username', 10, 'xiaoming236')
        name = conn.get('username')
        return Response(name)


# 注册
class Register(APIView):
    def post(self, request):

        # 用户名
        username = request.data.get('username')
        if not username:
            return Response({'code': 10010})
        # 格式是否正确
        name = re.match('\w{5,10}$', username)
        if not name:
            return Response({'code': 10011})
        # 是否唯一
        user = User.objects.filter(username=username).first()
        if user:
            return Response({'code': 10012})

        # 密码
        password = request.data.get('password')
        if not password:
            return Response({'code': 10013})
        # 格式是否正确
        pwd = re.match('\w{3,6}$', password)
        if not pwd:
            return Response({'code': 10014})

        # 手机号
        mobile = request.data.get('mobile')
        if not mobile:
            return Response({'code': 10016})
        phone = re.match('^1[3-9]\d{9}$', mobile)
        if not phone:
            return Response({'code': 10017})
        # 是否唯一
        sloe = User.objects.filter(mobile=mobile).first()
        if sloe:
            return Response({'code': 10018})

        # 验证码验证
        smscode = request.data.get('smscode')
        conn = get_redis()
        code = conn.get(mobile)
        if code:
            code = str(code, encoding='utf8')
        if smscode != code:
            return Response({'code': 10019})

        return Response({'code': 200, 'meg': '注册成功'})


# 验证码
class SendSms(APIView):
    def post(self, request):
        username = request.data.get('username')
        mobile = request.data.get('mobile')
        # 验证图片验证码,通过后发短信
        uid = request.data.get('uid')
        imagecode = request.data.get('imagecode')
        conn = get_redis()
        imgcode = conn.get(uid)
        if imgcode:
            imgcode = str(imgcode, encoding='utf8')
            if imagecode.lower() == imgcode.lower():
                codesms = random.randint(1000, 9999)
                res = send_message(mobile, str(codesms), 1)
                if res:
                    # 发生成功后存入redis
                    conn = get_redis()
                    conn.setex(mobile, 60, codesms)
                    # 判断是否同意协议
                    agree = request.data.get('agree')
                    if not agree:
                        return Response({'code': 10020})
                    # 注册功能
                    request.data['password'] = make_password(request.data.get('password'))
                    ser = UserModel(data=request.data)
                    if ser.is_valid():
                        ser.save()
                        token = jwt.encode({'data': {'username': username}},
                                           settings.SECRET_KEY, algorithm='HS256')
                        user = User.objects.filter(username=username).first()
                        return Response({'code': 200, 'meg': '注册成功', 'token': token, 'userid': user.id})
                    return Response({'code': 10021})


# 生成图片验证码
from utils.captcha.captcha import captcha


class CreateCaptcha(APIView):
    def get(self, request):
        uuid = request.GET.get('uuid')
        name, text, img = captcha.generate_captcha()
        # redis存储
        conn = get_redis()
        conn.set(uuid, text)
        return HttpResponse(img, content_type='image/jpg')


# class Login(APIView):
#     def post(self, request):
#         username = request.data.get('username')
#         password = request.data.get('password')
#         user = User.objects.filter(username=username).first()
#         if user:
#             if check_password(password, user.password):
#                 token = jwt.encode({'data': {'username': username, 'id': user.id}},
#                                    settings.SECRET_KEY, algorithm='HS256')
#                 return Response({'code': 200, 'token': token, 'userid': user.id})
#         return Response({"code": 20011})